package com.asurplus.business.service.impl;

import com.asurplus.business.entity.AppVersion;
import com.asurplus.business.mapper.AppVersionMapper;
import com.asurplus.business.service.AppVersionService;
import com.asurplus.common.layui.LayuiTableResult;
import com.asurplus.common.shiro.ShiroUtils;
import com.asurplus.common.utils.ResponseResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * app版本信息 服务实现类
 * </p>
 *
 * @author lizhou
 * @since 2021-01-01
 */
@Service
public class AppVersionServiceImpl extends ServiceImpl<AppVersionMapper, AppVersion> implements AppVersionService {

    @Override
    public LayuiTableResult list(Integer page, Integer limit, AppVersion appVersion) {
        LambdaQueryWrapper<AppVersion> queryWrapper = new LambdaQueryWrapper<>();
        if (null != appVersion.getVersionCode()) {
            queryWrapper.eq(AppVersion::getVersionCode, appVersion.getVersionCode());
        }
        if (null != appVersion.getForceUpdate()) {
            queryWrapper.eq(AppVersion::getForceUpdate, appVersion.getForceUpdate());
        }
        if (StringUtils.isNotBlank(appVersion.getAppName())) {
            queryWrapper.like(AppVersion::getAppName, appVersion.getAppName());
        }
        if (StringUtils.isNotBlank(appVersion.getVersionName())) {
            queryWrapper.like(AppVersion::getVersionName, appVersion.getVersionName());
        }
        if (StringUtils.isNotBlank(appVersion.getVersionInfo())) {
            queryWrapper.like(AppVersion::getVersionInfo, appVersion.getVersionInfo());
        }
        queryWrapper.orderByDesc(AppVersion::getVersionCode);
        return new LayuiTableResult<>(this.baseMapper.selectPage(new Page<>(page, limit), queryWrapper));
    }

    @Override
    public ResponseResult add(AppVersion appVersion) {
        if (null == appVersion) {
            return ResponseResult.error("数据错误");
        }
        LambdaQueryWrapper<AppVersion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppVersion::getAppName, appVersion.getAppName());
        queryWrapper.eq(AppVersion::getVersionName, appVersion.getVersionName());
        AppVersion dbObj = this.getOne(queryWrapper, false);
        if (null != dbObj) {
            return ResponseResult.error("该版本号已经存在");
        }
        appVersion.setCreateUser(ShiroUtils.getSysUserId());
        this.baseMapper.insert(appVersion);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult update(AppVersion appVersion) {
        if (null == appVersion || null == appVersion.getId() || 0 == appVersion.getId()) {
            return ResponseResult.error("数据错误");
        }
        LambdaQueryWrapper<AppVersion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(AppVersion::getId, appVersion.getId());
        queryWrapper.eq(AppVersion::getAppName, appVersion.getAppName());
        queryWrapper.eq(AppVersion::getVersionName, appVersion.getVersionName());
        AppVersion dbObj = this.getOne(queryWrapper, false);
        if (null != dbObj) {
            return ResponseResult.error("该版本号已经存在");
        }
        appVersion.setUpdateUser(ShiroUtils.getSysUserId());
        this.baseMapper.updateById(appVersion);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult delete(Integer id) {
        if (null == id || 0 == id) {
            return ResponseResult.error("数据错误");
        }
        this.baseMapper.deleteById(id);
        return ResponseResult.success();
    }
}
